<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>合并一个或多个数组</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-merge-recursive.html">array_merge_recursive</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-multisort.html">array_multisort</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.array-merge" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_merge</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">array_merge</span> &mdash; <span class="dc-title">合并一个或多个数组</span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-merge-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">array</span> <span class="methodname"><strong>array_merge</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array1</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$...</code></span>
  ] )</div>

    <p class="para rdfs-comment">
     <span class="function"><strong>array_merge()</strong></span> 将一个或多个数组的单元合并起来，一个数组中的值附加在前一个数组的后面。返回作为结果的数组。
    </p>
    <p class="para">
     如果输入的数组中有相同的字符串键名，则该键名后面的值将覆盖前一个值。然而，如果数组包含数字键名，后面的值将<em class="emphasis">不会</em>覆盖原来的值，而是附加到后面。
    </p>
    <p class="para">
     如果只给了一个数组并且该数组是数字索引的，则键名会以连续方式重新索引。
    </p>
   </div>

 <div class="refsect1 parameters" id="refsect1-function.array-merge-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">array1</code></em></dt>

     <dd>

      <p class="para">
       Initial array to merge.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">...</code></em></dt>

     <dd>

      <p class="para">
       Variable list of arrays to merge.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-merge-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回结果数组。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.array-merge-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.0.0</td>
       <td>
        <div class="warning"><strong class="warning">Warning</strong>
         <p class="para">
      <span class="function"><strong>array_merge()</strong></span> 的行为在 PHP 5 中被修改了。和 PHP 4
      不同，<span class="function"><strong>array_merge()</strong></span> 现在只接受
      <span class="type"><a href="language.types.array.html" class="type array">array</a></span> 类型的参数。不过可以用强制转换来合并其它类型。请看下面的例子。
         </p>
         <p class="para">
          <div class="example" id="example-4864">
           <p><strong>Example #1 <span class="function"><strong>array_merge()</strong></span> PHP 5 例子</strong></p>
           <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$beginning&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$end&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_merge</span><span style="color: #007700">((array)</span><span style="color: #0000BB">$beginning</span><span style="color: #007700">,&nbsp;(array)</span><span style="color: #0000BB">$end</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
           </div>

           <div class="example-contents"><p>以上例程会输出：</p></div>
           <div class="example-contents screen">
    <div class="cdata"><pre>
    Array
    (
        [0] =&gt; foo
        [1] =&gt; bar
    )
</pre></div>
           </div>
          </div>
         </p>
        </div>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.array-merge-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4865">
    <p><strong>Example #2 <span class="function"><strong>array_merge()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array1&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"color"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"a"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"color"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"shape"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"trapezoid"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_merge</span><span style="color: #007700">(</span><span style="color: #0000BB">$array1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [color] =&gt; green
    [0] =&gt; 2
    [1] =&gt; 4
    [2] =&gt; a
    [3] =&gt; b
    [shape] =&gt; trapezoid
    [4] =&gt; 4
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4866">
    <p><strong>Example #3 Simple <span class="function"><strong>array_merge()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array1&nbsp;</span><span style="color: #007700">=&nbsp;array();<br /></span><span style="color: #0000BB">$array2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"data"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_merge</span><span style="color: #007700">(</span><span style="color: #0000BB">$array1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     别忘了数字键名将会被重新编号！
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; data
)
</pre></div>
    </div>
    <div class="example-contents"><p>
     如果你想完全保留原有数组并只想新的数组附加到后面，用 <em>+</em> 运算符：
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array1&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'zero_a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'two_a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'three_a'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'one_b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'three_b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'four_b'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$array1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$array2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     The keys from the first array will be preserved. If an array key
     exists in both arrays, then the element from the first array will
     be used and the matching key&#039;s element from the second array will
     be ignored.
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(5) {
  [0]=&gt;
  string(6) &quot;zero_a&quot;
  [2]=&gt;
  string(5) &quot;two_a&quot;
  [3]=&gt;
  string(7) &quot;three_a&quot;
  [1]=&gt;
  string(5) &quot;one_b&quot;
  [4]=&gt;
  string(6) &quot;four_b&quot;
}
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-merge-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-merge-recursive.html" class="function" rel="rdfs-seeAlso">array_merge_recursive()</a> - 递归地合并一个或多个数组</span></li>
    <li class="member"><span class="function"><a href="function.array-combine.html" class="function" rel="rdfs-seeAlso">array_combine()</a> - 创建一个数组，用一个数组的值作为其键名，另一个数组的值作为其值</span></li>
    <li class="member"><a href="language.operators.array.html" class="link">array operators</a></li>
   </ul>
  </p>
 </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-merge-recursive.html">array_merge_recursive</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-multisort.html">array_multisort</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
